package com.evil.application.enums;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 自增值重置周期
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ResettingTypeEnum {

    /**
     * 自增值重置周期，0:不重置, 1:每年, 2:每季, 3:每月, 4:每周, 5:每日
     */
    UN_RESETTING(0, "不重置"),
    YEAR(1, "每年"),
    SEASON(2, "每季"),
    MONTH(3, "每月"),
    WEEK(4, "每周"),
    DAY(5, "每日"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ResettingTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ResettingTypeEnum isExistById(Integer id) {
        return ResettingTypeEnum.findById(id).orElseThrow(()
                -> new BusinessException(BaseEnum.ERROR_PARAMETER, "自增值重置周期参数错误"));
    }
}
